/**
 * 获取指定目录下所有文件中的中文
 * @type {string}
 */

//文件保存地址
const i18nFilePath = "E:\\temp\\i18n\\a.vue"

//读取文件目录
let projectPath = "D:\\yliyun\\workspace\\idea\\coffee\\lumiere-mall-admin-web\\src\\views\\gift";

/**
 *
 * @type {{includes: string[]}} 读取的文件类型
 */
let options = {
    includes: [".vue"]
}

const fs = require("fs");
const path = require("path");


let allFiles = [];
function loadAllFile(folderPath, allFiles, options){
    let files = fs.readdirSync(folderPath);
    for (let i = 0; i < files.length; i++) {
        let filePath = path.join(folderPath, files[i]);
        let extname = path.extname(filePath);
        let stat = fs.statSync(filePath);
        if(stat.isDirectory()){
            loadAllFile(filePath, allFiles, options);
        }else{
            if(options && options.includes && options.includes.indexOf(extname) > -1){
                allFiles.push(filePath);
            }
        }
    }
}

function getCnFromFile(filePath){
    let str = fs.readFileSync(filePath, {encoding: "utf-8"});
    if(str){
        let strArray = str.split("\n");
        for (let i = 0; i < strArray.length; i++) {
            let cn = strArray[i].replace(/[^\u4e00-\u9fa5]/gi, "");
            if(cn){
                fs.appendFileSync(i18nFilePath, '"' + cn + '"' + "\n");
            }
        }
    }
}

function genI18nFile(files){
    for (let i = 0; i < files.length; i++) {
        getCnFromFile(files[i]);
    }
}

function genI18nFiles(files){
    for (let i = 0; i < files.length; i++) {
        getTFromFile(files[i]);
    }
}
function getTFromFile(filePath){
    let rtnArr = [];
    let str = fs.readFileSync(filePath, {encoding: "utf-8"});
    if(str){
        let strArray = str.split("\n");
        for (let i = 0; i < strArray.length; i++) {
            rtnArr.push(replaceI18n(strArray[i]));
        }
    }
    rtnArr.forEach(function(s){

        let fileName = path.join(path.dirname(filePath), path.basename(filePath).replace(path.extname(filePath), 1+".vue"))
        fs.appendFileSync(fileName, s + "\n");
    });
}

const regex1 = /[\=]\"[\u4e00-\u9fa5]+\"[^\)]/m;
const regex2 = /[\=]\'[\u4e00-\u9fa5]+\'[^\)]/m;
const regex3 = /[\>][\u4e00-\u9fa5]+[/<]/m;
function replaceI18n(str){
    let m;
    let rtn = "";
    if ((m = regex1.exec(str)) !== null) {
        // The result can be accessed through the `m`-variable.
        m.forEach((match, groupIndex) => {
            // console.log(`Found match, group ${groupIndex}: ${match}`);
            let perfix = match.substr(0, 1);
            let suffix = match.substr(match.length-1, 1);
            let replaceStr = perfix + "'$t(" + match.substr(1, match.length - 2) + ")'" + suffix;
            rtn = str.replace(match, replaceStr);
        });
    }else  if ((m = regex2.exec(str)) !== null) {
        // The result can be accessed through the `m`-variable.
        m.forEach((match, groupIndex) => {
            // console.log(`Found match, group ${groupIndex}: ${match}`);
            let perfix = match.substr(0, 1);
            let suffix = match.substr(match.length-1, 1);
            let replaceStr = perfix + '"$t(' + match.substr(1, match.length - 2) + ')"' + suffix;
            rtn = str.replace(match, replaceStr);
        });
    }else  if ((m = regex3.exec(str)) !== null) {
        // The result can be accessed through the `m`-variable.
        m.forEach((match, groupIndex) => {
            // console.log(`Found match, group ${groupIndex}: ${match}`);
            let perfix = match.substr(0, 1);
            let suffix = match.substr(match.length-1, 1);
            let replaceStr = perfix + '{{$t("' + match.substr(1, match.length - 2) + '")}}' + suffix;
            rtn = str.replace(match, replaceStr);
        });
    }else{
        rtn = str;
    }
    return rtn;
}

loadAllFile(projectPath, allFiles, options);
// genI18nFile(allFiles);

genI18nFiles(allFiles)
